﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Windows;
using Dev_house_Project.GUI.WPF.Cache;
using Dev_house_Project.GUI.WPF.Converters;
using Dev_house_Project.GUI.WPF.ViewModel;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;
using Microsoft.Synchronization.Data.SqlServerCe;
using Visifire.Charts;

namespace Dev_house_Project.GUI.WPF
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainViewModel _model;
        private List<string> listSyncProgress = new List<string>();
        private List<string> list2SyncProgress = new List<string>();

        public MainWindow()
        {
            InitializeComponent();

            _model = new MainViewModel();
            
            DataContext = _model;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            LocalCacheSyncAgent agent = new LocalCacheSyncAgent();
            agent.Synchronize();

            //((SqlCeClientSyncProvider)agent.LocalProvider).SyncProgress += new EventHandler<SyncProgressEventArgs>(ShowProgress);
            //((DbServerSyncProvider)agent.RemoteProvider).SyncProgress += new EventHandler<SyncProgressEventArgs>(ShowProgress2);

            //DataTable table = new DataTable();
            //SqlCeDataAdapter ad = new SqlCeDataAdapter();
            //using (SqlCeConnection conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["Dev_house_Project.GUI.WPF.Properties.Settings.Clientdev_house_projectConnectionString"].ConnectionString))
            //{
            //    conn.Open();
            //    SqlCeCommand comm = new SqlCeCommand("Select * from CompaniesData", conn);
            //    ad.SelectCommand = comm;
            //    ad.Fill(table);
            //    conn.Close();
            //}

            //MessageBox.Show(table.Rows[0]["Count"].ToString());


            //using (SqlCeConnection conn = new SqlCeConnection(ConfigurationManager.ConnectionStrings["Dev_house_Project.GUI.WPF.Properties.Settings.Clientdev_house_projectConnectionString"].ConnectionString))
            //{
            //    conn.Open();
            //    SqlCeCommand comm = new SqlCeCommand("Update CompaniesData set Count=1010 where CompanyId=@ID", conn);
            //    comm.Parameters.AddWithValue("@ID", new Guid("29738c10-9f97-11de-8a39-0800200c9a66"));
            //    comm.ExecuteNonQuery();
            //    conn.Close();
            //}

            //SqlDataAdapter add = new SqlDataAdapter();
            //using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["Dev_house_Project.GUI.WPF.Properties.Settings.Serverdev_house_projectConnectionString"].ConnectionString))
            //{
            //    conn.Open();
            //    SqlCommand comm = new SqlCommand("Select * from CompaniesData", conn);                
            //    add.SelectCommand = comm;
            //    add.Fill(table);
            //    conn.Close();
            //}

            //MessageBox.Show(table.Rows[0]["Count"].ToString());
            var converter = new CompaniesToChartDataSeries();
            var series = (DataSeries) converter.Convert(_model.Companies, null, null, null);

            series.RenderAs = RenderAs.Column;
            Chart1.Series.Add((DataSeries) converter.Convert(_model.Companies, null, null, null));

            var series2 = series;
            series.RenderAs = RenderAs.Pie;
            Chart2.Series.Add(series2);
        }

        //public void ShowFailures(object syncAgent, ApplyChangeFailedEventArgs args)
        //{
        //    args.Action = ApplyAction.Continue;
        //}

        //public void ShowProgress(object syncAgent, SyncProgressEventArgs args)
        //{
        //    Report(args);
        //}

        //public void ShowProgress2(object syncAgent, SyncProgressEventArgs args)
        //{
        //    Report2(args);
        //}

        //public void Report(SyncProgressEventArgs args)
        //{
        //    string message = "Progress: ";

        //    switch (args.SyncStage)
        //    {
        //        case SyncStage.ApplyingInserts:
        //            message += "Applying insert for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            message += "(Applied:" + args.TableProgress.ChangesApplied.ToString() + "/Pending:" + args.TableProgress.ChangesPending.ToString() +
        //               "/Failed:" + args.TableProgress.ChangesFailed.ToString() + "/Total:" + args.TableProgress.TotalChanges.ToString() + ")";
        //            break;

        //        case SyncStage.ApplyingUpdates:
        //            message += "Applying update for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            message += "(Applied:" + args.TableProgress.ChangesApplied.ToString() + "/Pending:" + args.TableProgress.ChangesPending.ToString() +
        //                "/Failed:" + args.TableProgress.ChangesFailed.ToString() + "/Total:" + args.TableProgress.TotalChanges.ToString() + ")";
        //            break;

        //        case SyncStage.ApplyingDeletes:
        //            message += "Applying delete for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            message += "(Applied:" + args.TableProgress.ChangesApplied.ToString() + "/Pending:" + args.TableProgress.ChangesPending.ToString() +
        //                "/Failed:" + args.TableProgress.ChangesFailed.ToString() + "/Total:" + args.TableProgress.TotalChanges.ToString() + ")";
        //            break;

        //        case SyncStage.GettingInserts:
        //            message += "Enumerating inserts for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            break;

        //        case SyncStage.GettingUpdates:
        //            message += "Enumerating updates for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            break;

        //        case SyncStage.GettingDeletes:
        //            message += "Enumerating deletes for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            break;
        //    }

        //    listSyncProgress.Add(message);
        //}

        //public void Report2(SyncProgressEventArgs args)
        //{
        //    string message = "Progress: ";

        //    switch (args.SyncStage)
        //    {
        //        case SyncStage.ApplyingInserts:
        //            message += "Applying insert for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            message += "(Applied:" + args.TableProgress.ChangesApplied.ToString() + "/Pending:" + args.TableProgress.ChangesPending.ToString() +
        //               "/Failed:" + args.TableProgress.ChangesFailed.ToString() + "/Total:" + args.TableProgress.TotalChanges.ToString() + ")";
        //            break;

        //        case SyncStage.ApplyingUpdates:
        //            message += "Applying update for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            message += "(Applied:" + args.TableProgress.ChangesApplied.ToString() + "/Pending:" + args.TableProgress.ChangesPending.ToString() +
        //                "/Failed:" + args.TableProgress.ChangesFailed.ToString() + "/Total:" + args.TableProgress.TotalChanges.ToString() + ")";
        //            break;

        //        case SyncStage.ApplyingDeletes:
        //            message += "Applying delete for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            message += "(Applied:" + args.TableProgress.ChangesApplied.ToString() + "/Pending:" + args.TableProgress.ChangesPending.ToString() +
        //                "/Failed:" + args.TableProgress.ChangesFailed.ToString() + "/Total:" + args.TableProgress.TotalChanges.ToString() + ")";
        //            break;

        //        case SyncStage.GettingInserts:
        //            message += "Enumerating inserts for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            break;

        //        case SyncStage.GettingUpdates:
        //            message += "Enumerating updates for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            break;

        //        case SyncStage.GettingDeletes:
        //            message += "Enumerating deletes for table: " + args.TableMetadata.TableName;
        //            message += "[" + args.TableProgress.Inserts.ToString() + "|" + args.TableProgress.Updates.ToString() + "|" + args.TableProgress.Deletes.ToString() + "]";
        //            break;
        //    }

        //    list2SyncProgress.Add(message);
        //}

    }

}
